﻿

Ext.define('app.prm.project.CustomerProject', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    border: false,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    controller: 'project',
    requires: ['app.prm.project.ProjectController', 'app.prm.project.ViewModel'],
    viewModel: {
        type: 'projectModel'
    },
    initComponent: function () {
        var me = this;
        me.items = [
            me.getCustomersPanel(),
           // me.getHistoryGrid()
           me.getTabPanel()
        ];
        me.callParent();
    },
    getCustomersPanel: function () {
        if (!this.customerPanel) {


            //var store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/todo/GetTask' });
            var store = Ext.create("app.ux.PrmStore",
                {
                    url: '/api/services/app/user/GetAllCustomers', autoLoad: true, remoteSort: false,
                    sorters: [{
                        //先按年龄降序  
                        property: 'doingCount',
                        direction: 'DESC'
                    }, {
                        //后按名称升序  
                        property: 'name',
                        direction: 'ASC'
                    }]
                });
            var dataView = new Ext.view.View({
                store: store,
                tpl: [
                    '<tpl for=".">',
                        '<div style="margin:5px">',
                            '{name}[{doingCount}]',
                        '</div>',
                    '</tpl>'
                ],
                multiSelect: true,
                autoHeight: true,
                trackOver: true,
                overItemCls: 'x-item-over',
                itemSelector: 'div',
                prepareData: function (data) {
                    return data;
                },
                listeners: {
                    itemclick: 'onFollowCustomerClick'
                }
            });

            this.customerPanel = Ext.create('Ext.panel.Panel', {
                title: '客户列表', region: 'west', width: 100,
                layout: 'fit',
                items: [dataView]
            })
        }
        return this.customerPanel;

    },
    getTabPanel: function () {
        if (!this.tabPanel) {
            this.tabPanel = Ext.create("Ext.tab.Panel", {
                region: 'center',
                items: [
                    this.getProjectTab(),
                    this.getInquiryTab()
                ]
            });
        }
        return this.tabPanel;
    },
    getProjectTab: function () {
        if (!this.proTab) {
            var me = this;
            this.proTab = Ext.create('Ext.panel.Panel', {
                title: '&nbsp;&nbsp;项目&nbsp;&nbsp;',
                layout: 'border',
                items: [
                     me.getProjectSearchPanel(), me.getProjectGrid()
                ]
            })
        }
        return this.proTab;

    },
    getProjectGrid: function () {
        if (!this.projectGrid) {
            var me = this;
            this.projectGrid = Ext.create("Ext.grid.Panel", {
                region: 'center',
                forceFit: false,
                tbar: [
                    {
                        text: '所有', handler: 'onFollowProjectAllSearch'
                    },
                     {
                         text: '进行中', handler: 'onFollowProjectDoingSearch'
                     },
                    '->',
                     {
                         text: '重载',
                         glyph: 0xf021, handler: 'onFollowProjectStartSearch'
                     }
                ],
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/project/GetFollowerProjects', autoLoad: false }),
                columns: [
                    new Ext.grid.RowNumberer({ header: '#' }),
                    { header: '产品号', dataIndex: 'productNo' },
                    { header: '项目号', dataIndex: 'projectNo' },
                    { header: '项目名称', dataIndex: 'projectName' },
                    { header: '状态', dataIndex: 'status' },
                    { header: '属地', dataIndex: 'rdCenter' },
                    { header: '项目经理', dataIndex: 'pm' },
                    { header: '进度', dataIndex: 'schedule' },
                    //{ header: '延期', dataIndex: 'delayPeriod' },
                    { header: '计划开始时间', dataIndex: 'planStartTime' },
                    { header: '计划结束时间', dataIndex: 'planEndTime' },
                    { header: '实际开始时间', dataIndex: 'actualStartTime', hidden: true },
                    { header: '预期结束时间', dataIndex: 'actualEndTime', hidden: true }
                ],
                listeners: {
                    rowdblclick: 'onDbClick'
                }
            })
        }
        return this.projectGrid;
    },
    getProjectSearchPanel: function () {
        if (!this.proSearchPanel) {
            this.proSearchPanel = Ext.create("Ext.form.Panel", {
                title: '更多检索项',
                collapsible: true,
                collapsed: true,
                //split: true,
                height: 250
                , region: 'north',
                layout: 'form',
                defaults: { labelAlign: 'right' },
                items: [
                    { xtype: 'hidden', name: 'customer' },
                    {
                        fieldLabel: '项目阶段过滤方式',
                        xtype: 'radiogroup',
                        items: [{ boxLabel: '忽略', name: 'phaseStatus', inputValue: -1, checked: true }, { name: 'phaseStatus', inputValue: 0, boxLabel: '包含' }, { name: 'phaseStatus', inputValue: 1, boxLabel: '已完成' }]
                    }
                    ,
                    {
                        xtype: 'checkboxgroup',
                        reference: '',
                        fieldLabel: '项目阶段',
                        columns: 4,
                        items: [
                        ],
                        listeners: {
                            afterrender: function (me) {
                                Ext.create("app.ux.PrmStore",
                                {
                                    url: '/api/services/app/phaseInfo/GetAll?parentId=0', autoLoad: true, remoteSort: true,
                                    listeners: {
                                        load: function (e, records, successful, eOpts) {
                                            for (var i = 0; i < records.length; i++) {
                                                var r = records[i];
                                                me.items.add(Ext.create('Ext.form.field.Checkbox', {
                                                    boxLabel: r.get('text'),
                                                    name: 'phase',
                                                    inputValue: Math.abs(r.get('id')),
                                                    checked: true
                                                }));
                                            }

                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        xtype: 'checkboxgroup',
                        fieldLabel: '项目状态',
                        items: [
                         { boxLabel: localize('Ready'), name: 'status', inputValue: 50, checked: true },
                            { boxLabel: localize('Processing'), name: 'status', inputValue: 10, checked: true },
                            { boxLabel: localize('Complete'), name: 'status', inputValue: 20 },
                            { boxLabel: localize('Pause'), name: 'status', inputValue: 30, checked: true },
                            { boxLabel: localize('End'), name: 'status', inputValue: 40 }
                        ]
                    },
                    {
                        fieldLabel: '项目时间段',
                        xtype: 'fieldcontainer', layout: 'column',
                        items: [
                            {
                                xtype: 'datefield', name: 'dts',
                                format: 'Y-m-d', width: 150, value: Ext.Date.add(new Date(), Ext.Date.YEAR, -1)
                            }, { xtype: 'displayfield', value: '-' }, {
                                xtype: 'datefield', name: 'dte',
                                format: 'Y-m-d', width: 150, value: new Date()
                            }
                        ]
                    }
                ]
            });
        }
        return this.proSearchPanel;
    },
    getInquiryTab: function () {
        if (!this.inquiryTab) {
            this.inquiryTab = Ext.create('Ext.panel.Panel', {
                title: '&nbsp;&nbsp;询盘&nbsp;&nbsp;',
                layout: 'border',
                items: [
                    this.getInquirySearchPanel(),
                    this.getInquiryGrid()
                ]
            })
        }
        return this.inquiryTab
    },
    getInquiryGrid: function () {
        if (!this.inquiryGrid) {
            var me = this;
            this.inquiryGrid = Ext.create("Ext.grid.Panel", {
                region: 'center',
                forceFit: false,
                tbar: [
                    '->',
                     {
                         text: '重载',
                         glyph: 0xf021, handler: 'onFollowInquiryStartSearch'
                     }
                ],
                store: Ext.create("app.ux.PageInquiryStore", { url: '/api/services/app/Inquiry/GetAll', autoLoad: false }),
                columns: [
                 new Ext.grid.RowNumberer(),
                { header: '日期', dataIndex: 'createOn', renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                { header: '化学名', dataIndex: 'subName' },
                { header: 'CAS', dataIndex: 'subCas' },
                { header: '要求交期', dataIndex: 'targetDate', renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                { header: '项目流水号', dataIndex: 'projectNo' },
                { header: '九洲代号', dataIndex: 'inquiryNo' },
                 { header: '状态', dataIndex: 'statusStr' },
                 { header: '销售', dataIndex: 'createBy' },
                 { header: '负责人', dataIndex: 'adminName' },
                { header: '客户名称', dataIndex: 'customerName' },
                { header: '项目名称', dataIndex: 'projectName' },
                { header: '项目类型', dataIndex: 'projectType' },
                { header: '产品号', dataIndex: 'productName' }
                ],
                listeners: {
                    rowdblclick: function (grid, record) {
                        var inquiryid = record.get('Id');
                        var win = Ext.create("app.prm.inquiry.view",
                           {
                               title: '询盘查看',
                               inquiryId: record.get('inquiryId'),
                               subId: record.get('subId')
                           });
                        win.show();
                    }
                }
            })
        }
        return this.inquiryGrid;
    },
    getInquirySearchPanel: function () {
        if (!this.inquirySearchPanel) {
            this.inquirySearchPanel = Ext.create("Ext.form.Panel", {
                title: '更多检索项',
                collapsible: true,
                collapsed: true,
                //split: true,
                height: 100
                , region: 'north',
                layout: 'form',
                defaults: { labelAlign: 'right' },
                items: [
                    { xtype: 'hidden', name: 'CustomerName' },
                    {
                        fieldLabel: '立项状态',
                        xtype: 'radiogroup',
                        items: [{ boxLabel: 'All', name: 'proStatus', inputValue: 0, checked: true },
                            { name: 'proStatus', inputValue: -1, boxLabel: '未立项' },
                            { name: 'proStatus', inputValue: 1, boxLabel: '已立项' }]
                    },
                    {
                        fieldLabel: '时间段',
                        xtype: 'fieldcontainer', layout: 'column',
                        items: [
                            {
                                xtype: 'datefield', name: 'dts',
                                format: 'Y-m-d', width: 150, value: Ext.Date.add(new Date(), Ext.Date.YEAR, -1)
                            }, { xtype: 'displayfield', value: '-' }, {
                                xtype: 'datefield', name: 'dte',
                                format: 'Y-m-d', width: 150, value: new Date()
                            }
                        ]
                    }
                ]
            });
        }
        return this.inquirySearchPanel;
    },
    renderHistoryWin: function (customerName) {
        var main = this;

        var historyGrid =
                 Ext.create('Ext.window.Window', {
                     modal: true,
                     title: '' + customerName,
                     width: 400,
                     height: 250,
                     layout: 'fit',
                     items: Ext.create("Ext.grid.Panel", {
                         region: 'center',
                         forceFit: false,
                         store: Ext.create("app.ux.PrmStore", {
                             url: '/api/services/app/project/GetHistoryProInquiry', autoLoad: true,
                             remoteSort: false,
                             filter: { customer: customerName },
                             sorters: [{
                                 //先按年龄降序  
                                 property: 'year',
                                 direction: 'DESC'
                             }]
                         }),

                         columns: [
                             new Ext.grid.RowNumberer({ header: '#' }),
                             { header: '年份', dataIndex: 'year' },
                             {
                                 header: '项目数', dataIndex: 'proCount', renderer:
                                            function (val) {
                                                return '<span style="color:blue">' + val + '</span>'
                                            }
                             },
                             {
                                 header: '询盘数', dataIndex: 'inquiryCount', renderer:
                                        function (val) {
                                            return '<span style="color:blue">' + val + '</span>'
                                        }
                             }
                         ],
                         listeners: {
                             celldblclick: function (grid, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                                 var tab = main.getTabPanel(),
                                    InquiryTab = main.getInquiryTab(),
                                    ProjectTab = main.getProjectTab(),
                                form1 = main.getProjectSearchPanel(),
                                form2 = main.getInquirySearchPanel();
                                 var dataIndex = grid.getColumnManager().columns[cellIndex].dataIndex;
                                 if ('proCount' == dataIndex) {
                                     tab.getLayout().setActiveItem(ProjectTab);

                                 }
                                 else if ('inquiryCount' == dataIndex) {

                                     tab.getLayout().setActiveItem(InquiryTab);
                                 }
                                 else {
                                     return;
                                 }

                                
                                 var year = record.get('year');
                                 var dts = new Date(year, 1, 1);
                                 var dte = new Date(year + 1, 1, 1);
                                 form1.getForm().setValues({ dts: dts, dte: dte });
                                 form2.getForm().setValues({ dts: dts, dte: dte });
                                 main.getController().onFollowProjectStartSearch();
                                 main.getController().onFollowInquiryStartSearch();
                                 historyGrid.close();
                             },
                         }
                     })
                 })

        historyGrid.show();
    }
})